package com.huhu.myharmony.ble.slice;

import com.huhu.myharmony.BaseAbilitySlice;
import com.huhu.myharmony.ResourceTable;
import com.huhu.myharmony.ble.ProtocolEntity;
import com.huhu.myharmony.ble.controller.BleHelper;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.bluetooth.ble.BleScanResult;
import ohos.hiviewdfx.HiLog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class BleDetailAbilitySlice extends BaseAbilitySlice implements BleHelper.BleManagerCallback {

    private Image back;
    private Button refresh;
    private Text tvReportValue1;
    private Text tvThresholdValue1;
    private TextField writeTf;

    private Button writeButton;
    private Text writeText;
    private Text readText;

    private String mTagId;
    private String mWriteTagId = "";
    private List<String> tagIds = new ArrayList<>();
    private String mReadStr = "";
    private String mWriteStr = "";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_ble_detail);

        initView();
        initData();
        initListener();

    }

    private void initView() {
        back = (Image) findComponentById(ResourceTable.Id_back);
        refresh = (Button) findComponentById(ResourceTable.Id_refresh);

        tvReportValue1 = (Text) findComponentById(ResourceTable.Id_tvReportValue1);
        tvThresholdValue1 = (Text) findComponentById(ResourceTable.Id_tvThresholdValue1);
        writeTf = (TextField) findComponentById(ResourceTable.Id_writeTf);

        writeButton = (Button) findComponentById(ResourceTable.Id_writeButton);

        writeText = (Text) findComponentById(ResourceTable.Id_writeText);
        readText = (Text) findComponentById(ResourceTable.Id_readText);
    }

    private void initData() {
        BleHelper.getInstance().registerBleManagerCallback(this);
        tagIds.add("0001");
        tagIds.add("0002");
        mWriteTagId = "0002";
        BleHelper.getInstance().readInitiativeByTags(tagIds);
    }


    private void initListener() {
        back.setClickedListener(component -> terminate());
        refresh.setClickedListener(component ->
                BleHelper.getInstance().readInitiativeByTags(tagIds)
        );
        writeButton.setClickedListener(component -> {
                    if (writeTf.getText().isEmpty()) return;
                    mReadStr = "";
                    mWriteStr = "";
                    BleHelper.getInstance().writeBleByTag(mWriteTagId, writeTf.getText());

//                    getUITaskDispatcher().delayDispatch(() -> BleHelper.getInstance().readInitiativeByTags(Collections.singletonList(mWriteTagId)), 100);
                }
        );
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void scanResultCallback(BleScanResult var1) {

    }

    @Override
    public void connectCallback(int var1, String deviceName) {

    }

    @Override
    public void characteristicChangedCallback(ProtocolEntity protocolEntity, String hexStr) {
        getUITaskDispatcher().syncDispatch(() -> {
            HiLog.debug(loglabel, "上报  hexStr:" + hexStr);
            mReadStr = mReadStr + "\n" + hexStr;
            readText.setText(mReadStr);
            Map<Integer, ProtocolEntity.DeviceData> deviceListMap = protocolEntity.getDeviceListMap();
            for (Integer key : deviceListMap.keySet()) {
                ProtocolEntity.DeviceData deviceData = deviceListMap.get(key);
                if (key == 1) {
                    tvReportValue1.setText(deviceData.getValue() + "");
                } else if (key == 2) {
                    tvThresholdValue1.setText(deviceData.getValue() + "");
                }

            }
        });
    }

    @Override
    public void characteristicWriteCallback(String hexStr) {
        getUITaskDispatcher().syncDispatch(() -> {
            HiLog.debug(loglabel, "写入成功  hexStr:" + hexStr);
            mWriteStr = mWriteStr + "\n" + hexStr;
            writeText.setText(mWriteStr);
        });
    }
}
